package com.hncj.yqfk.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hncj.yqfk.entity.Point;
import com.hncj.yqfk.service.RygjService;
import com.hncj.yqfk.service.impl.RygjServiceImpl;
import com.hncj.yqfk.utils.JsonResult;
import com.hncj.yqfk.utils.NameConvert;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class RygjController {

    @Autowired
    RygjService rygjService=new RygjServiceImpl();

    @GetMapping("/maxrl")
    @ResponseBody
    public JsonResult maxrl(){
        //通过Service得到数据并返回即可
        return rygjService.GetRank();
    }

    @GetMapping("/cxxx")
    @ResponseBody
    public JsonResult cxxx(@RequestParam String lymc, @RequestParam String stime, @RequestParam String stype){
        if ("0".equals(lymc)){
            return rygjService.GetSchoolRndByDayAndType(stime.split(" - ")[0],stime.split(" - ")[1],stype);
        }else {
//            判断是否查询的是大门
            if (NameConvert.isbigdoor(lymc)){
                return rygjService.GetInfoByNaDaTy(lymc,stime.split(" - ")[0],stime.split(" - ")[1],stype);
            }else {
                return rygjService.GetInfoByBhAndDayAndType(lymc,stime.split(" - ")[0],stime.split(" - ")[1],stype);
            }
        }
    }

    @GetMapping("/gjcs/cslist")
    public JsonResult cslist() {
        //通过Service得到数据并返回即可
        return rygjService.GetAllMcBh();
    }

    @GetMapping("/rygj")
    @ResponseBody
    public JsonResult getLineByHttpClient(@RequestParam String username, @RequestParam String stime) {

        System.out.println(username + stime);
        String url = "https://gis.hncj.edu.cn/cmgis-server/map/route/v3/bind/road/1";
        String token = "Basic eHk6NTU5OWI1ZmNlNzFjNDcwNzg4M2E3YTU5NDc1ZGQxODA=";

        // 一个浏览器
        HttpClient httpClient = HttpClientBuilder.create().build();
        // 建立HttpPost对象
        HttpPost httpPost = new HttpPost(url);
        httpPost.setHeader("Authorization", token);

        //请求体格式
        httpPost.setHeader("Accept", "application/json");
        httpPost.setHeader("Content-Type", "application/json");
        httpPost.setHeader("Authorization", token);
        String charSet = "UTF-8";
        //调用业务层，请求数据
        Map<String, Object> map = rygjService.GetByDayAndUsername(username, stime.split(" - ")[0], stime.split(" - ")[1]);

        //对回调码校验，200为成功，202为未找到相关信息
        if (map.get("code").equals(202)) {
            return JsonResult.custom(202, "未找到相关信息", new Date(), null);
        } else {

            //创建场所坐标列表，用于规划路径
            List<Point> pointList = (List<Point>) map.get("cszb");

            //创建json
            JSONArray arr = JSON.parseArray(JSONObject.toJSONString(pointList));

            String jsonStr = arr.toJSONString();
            StringEntity entity = new StringEntity(jsonStr, charSet);//模拟json请求
            httpPost.setEntity(entity);

            HttpResponse response = null;

            Map<String, Object> res = new HashMap<>();
            try {
                response = httpClient.execute(httpPost);
                StatusLine status = response.getStatusLine();

                String jsonString = null;
                int state = status.getStatusCode();
                //响应状态码为200
                if (state == HttpStatus.SC_OK) {
                    HttpEntity responseEntity = response.getEntity();
                    jsonString = EntityUtils.toString(responseEntity);
                    Map mapTypes = JSON.parseObject(jsonString);
                    System.out.println("这个是用JSON类的parseObject来解析JSON字符串!!!");
                    for (Object obj : mapTypes.keySet()) {
                        if (obj.equals("data")) {
                            res.put("gjzb", mapTypes.get(obj));
                        }
                    }

                    res.put("name", map.get("name"));
                    res.put("num", map.get("num"));
                    res.put("jccs", map.get("jccs"));
                    res.put("cszb", map.get("cszb"));

                    System.out.println("请求成功!");
                    return JsonResult.custom(200, "", new Date(), res);

                } else {
                    HttpEntity responseEntity = response.getEntity();
                    jsonString = EntityUtils.toString(responseEntity);
                    System.out.println(jsonString);
                    System.out.println("请求失败!");
                    return JsonResult.custom(202, "请求失败", new Date(), res);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return JsonResult.custom(202, "请求失败", new Date(), res);
        }

    }
}